import { observable, action, toJS } from 'mobx';
import * as ManuallydispatchAPI from '../apis/manuallyDispatch'
import { WeaForm ,WeaTableNew} from 'comsMobx';
const {TableStore} = WeaTableNew;

export default class ManuallyDispatch{

	//请求列表
	@observable tableStore = new TableStore();
	/** 列表store */
	@observable title = '';
	@observable loading = false; // 加载中状态
	@observable sessionkey = ''; // 表格sessionkey

	/** 高级查询store */
	@observable form = new WeaForm(); // 表单
	@observable conditioninfo = []; // 高级搜索条件

	@observable requestids = '';
	@observable hasRight=true;
	@observable showSearchAd = false;
	@observable conditionLoading = false;
	@observable quickSearchValue = '';
	@observable visible = false;
	@observable dialogtitle = '';
	@observable rightMenu = [];//页面右键

	@observable form1 = new WeaForm();//新增或编辑规则
	@observable chooseDialogCon1 = [];//新增或编辑规则字段
	@observable chooseDialogRM1 = [];//弹出框右键

	@action doSearch(params = {}) {
		this.loading=true;
		ManuallydispatchAPI.doSearch(params).then(data => {
			this.hasRight = data.hasRight;
			if(data.hasRight){
				this.tableStore.getDatas(data.sessionkey);
				this.sessionkey = data.sessionkey;
			}
			this.loading=false;
		});
		this.loading=false;
	}

	@action getRightMenu(params = {}) {
		ManuallydispatchAPI.getRightMenu(params).then(data => {
			this.rightMenu = data.rightMenus;
		});
		return this;
	}

	@action getConditionInfo(params = {}) {
		this.conditionLoading = true;
		ManuallydispatchAPI.getConditionInfo(params).then(data => {
			this.conditionLoading = false;
			const { isFormInit } = this.form;
			if (isFormInit) {
				// 如果初始化过表单，需要重新初始化一份表单，并在同一周期置空数据
				// 需要暂存数据的可以考虑多份 ComsStore 继承
				this.form = new WeaForm();
				this.conditioninfo = [];
			}
			this.conditioninfo = data.condition;
			this.form.initFormFields(data.condition);
			const keys = Object.keys(params);
			if (keys.length > 1 && this.form.isFormInit) {
				const fields = {};
				keys.forEach(key => {
					if (key !== 'viewcondition' && key !== 'groupid') {
						fields[key] = { value: params[key] };
					}
				});
				this.form.updateFields(fields);
			}
		});
		return this;
	}

	@action setQuickSearchValue = quickSearchValue => {
		this.quickSearchValue = quickSearchValue;
	}

	@action setShowSearchAd = showSearchAd => {
		this.showSearchAd = showSearchAd;
	}

	@action setRequestids = requestids => {
		this.requestids = requestids;
	}

	@action setVisible = visible => {
		this.visible = visible;
	}

	@action setDialogType (params = {}) {
		//派工或者批量派工
		ManuallydispatchAPI.getchooseDialogCon(params).then(data => {
			this.chooseDialogCon1 = data.conditioninfo;
			this.form1.initFormFields(data.conditioninfo);
			this.dialogtitle = data.dialogtitle;
			this.chooseDialogRM1 = data.rightMenu;
		})
	}

	//提交手动派工
	@action onSubmit(params={}){
		ManuallydispatchAPI.onSubmit(params).then(data =>{
			this.tableStore.getDatas(data.sessionkey1);
			this.sessionkey = data.sessionkey1;
		});
	}
}
